<?php

namespace common\models\shop;

use common\models\business\BusinessUser;
use Yii;

/**
 * This is the model class for table "shop_order".
 *
 * @property string $id
 * @property string $from_user
 * @property string $ordersn
 * @property string $price
 * @property integer $status
 * @property integer $sendtype
 * @property integer $paytype
 * @property string $transid
 * @property string $remark
 * @property string $address
 * @property string $expresscom
 * @property string $expresssn
 * @property string $express
 * @property string $goodsprice
 * @property string $dispatchprice
 * @property integer $dispatch
 * @property string $paydetail
 * @property string $createtime
 * @property integer $sendstatus
 */
class ShopOrder extends \yii\db\ActiveRecord
{


    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '商品订单',      //用于修改了什么的显示
        'mainfield' => 'ordersn',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];


    //-2支付失败  -1取消状态，0普通状态，1为已付款，2为已发货，3为已收货，4订单完结(已评价)
    const STATUS_PAYFAIL = -2;
    const STATUS_CANCAL = -1;
    const STATUS_MANAL = 0;
    const STATUS_PAY = 1;
    const STATUS_SEND = 2;
    const STATUS_RECEIVE = 3;
    const STATUS_FINISH = 4;
    const STATUS_REFOUND = 5;
    const STATUS_REFOUND_PASS = 6;
    const STATUS_REFOUND_NOPASS = 7;
    public static $statues = [
        self::STATUS_PAYFAIL => '支付失败',
        self::STATUS_CANCAL => '取消',
        self::STATUS_MANAL => '待支付',
        self::STATUS_PAY => '已付款',
        self::STATUS_SEND => '已发货',
        self::STATUS_RECEIVE => '已收货',
        self::STATUS_REFOUND => '退款申请',
        self::STATUS_REFOUND_PASS => '退款审核通过',
        self::STATUS_REFOUND_NOPASS => '退款审核不通过',
//        self::STATUS_FINISH => '已评价',
    ];



    //1为快递，2为自提
    const SEND_DISPATCH = 1;
    const SEND_TAKE = 2;
    public static $sendtype = [
        self::SEND_DISPATCH => '快递',
        self::SEND_TAKE => '自提',
    ];



    const WECHAT_PAY = 1;
    const NO_PAY = 0;
    public static $paytype = [
        self::WECHAT_PAY => '微信支付',
        self::NO_PAY => '未支付',
    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'shop_order';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ordersn', 'price', 'paytype'], 'required'],
            [['userid','status', 'sendtype', 'paytype', 'dispatch', 'sendstatus','paytime'], 'integer'],
            [['goodsprice', 'dispatchprice'], 'number'],
            [['from_user', 'expresssn'], 'string', 'max' => 50],
            [['ordersn'], 'string', 'max' => 20],
            [['price'], 'string', 'max' => 10],
            [['transid', 'expresscom'], 'string', 'max' => 30],
            [['remark','refundfail_remark'], 'string', 'max' => 1000],
            [['address'], 'string', 'max' => 1024],
            [['express'], 'string', 'max' => 200],
            [['createtime'],'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'userid' => '用户id',
            'from_user' => 'openid',
            'ordersn' => '订单号',
            'price' => '订单金额',
            'status' => '状态',
            'sendtype' => '配送方式',
            'paytype' => '支付方式',
            'transid' => '支付平台流水',
            'remark' => '备注',
            'address' => '地址',
            'expresscom' => '快递公司',
            'expresssn' => '快递单号',
            'express' => '快递',
            'goodsprice' => '商品价格',
            'dispatchprice' => '运费',
            'dispatch' => '配送方式',
            'paydetail' => '支付详情',
            'createtime' => '下单时间',
            'paytime' => '付款时间',
            'sendstatus' => '发货状态',
            'refundfail_remark' => '退款审核失败原因'
        ];
    }


    /**
     * @return \yii\db\ActiveQuery
     * 获取购买用户
     */
    public function getUser()
    {
        return $this->hasOne(BusinessUser::className(),['openid'=>'from_user']);
    }


    /**
     * @return $this
     * 获取订单商品
     */
    public function  getGoods()
    {
        return $this->hasMany(ShopGoods::className(),['id'=>'orderid'])
            ->viaTable(ShopOrderGoods::tableName(),['goodsid'=>'id']);
    }


    public function getDispatcha()
    {
        return $this->hasOne(ShopDispatch::className(),['id'=>'dispatch']);

    }

}
